home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Clean 1.2.4 / Small Demos / hamming.icl < prev    next >
Encoding:
Text File  |  1995-03-10  |  949 b   |  40 lines  |  [TEXT/3PRM]

  1. module hamming
  2.  
  3. /*
  4. The Hamming Function.
  5.  
  6. The result of this program is a list of the first NrElements numbers
  7. having only 2, 3 and 5 as prime factors. Result:
  8.  
  9.     [1,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,...].
  10.  
  11. Run the program with the Show Constructors option on (Application options)
  12. */
  13.  
  14. import StdEnv
  15.  
  16. /*    Ham (the Hamming function) returns an infinite list of numbers
  17.     having two, three and five as only prime factors by recursively
  18.     mapping the functions (*I 2), (*I 3) and (*I 5) over the list of
  19.     Hamming numbers found sofar and merging these lists into one.    
  20. */
  21.  
  22. Ham::[Int]
  23. Ham    =    y
  24. where
  25.     y    =    [1:merge (merge (map ((*) 2) y) (map ((*) 3) y)) (map ((*) 5) y)]
  26.  
  27.     merge [] []    =  []
  28.     merge f     []    =  f
  29.     merge [] f    =  f
  30.     merge f=:[a:b]    g=:[c:d]
  31.         | a<c        =  [a:merge b g]
  32.         | a==c        =  merge f d
  33.         | otherwise    =  [c: merge f d]
  34.                                     
  35. Start::[Int]
  36. Start = take NrElements Ham
  37.  
  38. NrElements :== 300    // The number of Hamming numbers to be calculated.
  39.  
  40.